package cn.lijida.weiwei.favor.modules.home.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.lijida.weiwei.favor.modules.home.entity.FavorHome;
import cn.lijida.weiwei.favor.modules.home.entity.FavorHomeMember;
import cn.lijida.weiwei.favor.modules.home.mapper.FavorHomeMemberMapper;
import cn.lijida.weiwei.favor.modules.home.service.IFavorHomeMemberService;
import cn.lijida.weiwei.favor.modules.home.service.IFavorHomeService;

/**
 * <p>
 * 家庭成员关联表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-12-28
 */
@Service
public class FavorHomeMemberServiceImpl extends ServiceImpl<FavorHomeMemberMapper, FavorHomeMember> implements IFavorHomeMemberService {

	@Autowired
	private IFavorHomeService homeService;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void join(Long homeId, Long memberId) throws Exception {
		
		if(homeId == null || memberId == null){
			throw new Exception("参数异常");
		}
		
		//查询家庭
		FavorHome homeEntity = homeService.getById(homeId);
		if(homeEntity == null){
			throw new Exception("家庭不存在");
		}
		
		//管理员直接加入家庭，其他人需要查询邀请记录
		if(memberId.equals(homeEntity.getManageMember())){
			FavorHomeMember entity = new FavorHomeMember();
			entity.setHomeId(homeId);
			entity.setMemberId(memberId);
			boolean status = this.save(entity );
			if(!status){
				throw new Exception("加入家庭失败");
			}
		}
	}

}
